package com.baosight.sczx.common.zm.basetools.listener;

import java.util.Calendar;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
//import com.baosight.sczx.common.zm.cInterface.cf.MessageUtil;

/**
 * 定时活动
 * <p>Title: SystemAutoRun.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 李红星
 * @version 1.0
 * <pre>Histroy:
 *          2016-10-16 12:41:16  李红星  Create
 *</pre>
 */
public class SystemAutoRun extends TimerTask {
	
	@SuppressWarnings("unused")
	private ServletContext context;
	private static boolean isRunning = false;
	
	public SystemAutoRun(ServletContext context){
		this.context = context;
	}
	
	/**
	 * 开始
	 * <p>Function: run</p>
	 * <p>Description: </p>
	 * <p>return value: SystemAutoRun</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6上午05:42:16 严洪浩
	 * </p>
	 *
	 */
	@Override  
	public void run() {
		if(!isRunning){
			Calendar calPriv = Calendar.getInstance();
			int ihour = calPriv.get(Calendar.HOUR_OF_DAY);
			int iminute = calPriv.get(Calendar.MINUTE);
			if ((ihour == 1) && (iminute < 15)) {
				Runtime.getRuntime().gc();
			}
			isRunning = true;
			
			try {
				this.myTask();
			} catch (Exception e) {
				System.err.println("执行任务期间发生异常！" + e.getMessage());
			}
			
			isRunning = false;
		}
	}
	
	/**
	 * 里面添加具体方法
	 * 所有方法必须做异常处理，否则会卡住定时活动
	 * <p>Function: myTask</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6上午05:47:21 严洪浩
	 * </p>
	 *
	 */
	private void myTask(){
		System.out.println(">>>> 开始活动:  .............." + StaticTools.getDateTime());
		
        //煤质库存自动结转
		//added by 李红星
		//2016-10-16
/*		StockDayCarryForward sdcf=new StockDayCarryForward();
		try {
			sdcf.stockDayCarryForward();
		} catch (Exception e) {
			System.out.println("定时活动:  自动生成煤质库存结转记录失败！" + e.getMessage());
		}*/
		
		//其他样主表生成
		try{
			GetOtherCoalSampleTestResult oc=new GetOtherCoalSampleTestResult();
			oc.generateMain();
		}catch(Exception e){
			System.out.println("定时活动:  自动生成其他样主表记录失败！" + e.getMessage());
			e.printStackTrace();
		}
		
		//抽查样数据提取
/*		try{
			GetOtherCoalSampleTestResult oc=new GetOtherCoalSampleTestResult();
			oc.getOtherCoalSampleTestResult();
		}catch(Exception e){
			System.out.println("定时活动:  自动提取其他样化验记录失败！" + e.getMessage());
			e.printStackTrace();
		}*/
		
		
//		
//		//发送预警消息
//		//added by wangkun
//		//2011-08-29
//		GenerateAlarmPointMessage alarmPoint = new GenerateAlarmPointMessage();
//		alarmPoint.setDao(dao);
//		int value = alarmPoint.sendAlarmMessage();
//		if(value != 1)
//		{
//			System.out.println("定时活动:  发送预警消息失败！");
//		}
//		
//		//分解消息
//		//added by wangkun
//		//2011-08-18
//		//MessageUtil messageUtil = new MessageUtil();
//		messageUtil.setDao(dao);
//		value = messageUtil.decomposeMessage();
//		if(value != 1)
//		{
//			System.out.println("定时活动:  分解消息失败！" + value);
//		}
//		
//		//设备检修提醒
//		//added by yanhonghao
//		//2011-11-11
//		EquipmentMaintenance equipmentMaintenance = new EquipmentMaintenance();
//		equipmentMaintenance.setDao(dao);
//		try {
//			equipmentMaintenance.maintenance();
//		} catch (Exception e) {
//			System.out.println("定时活动:  设备检修提醒失败！" + e.getMessage());
//		}
//		
//        //自动生成下月定额
//		//added by wangkun
//		//2012-02-22
//		GenerateMonthCharge charge = new GenerateMonthCharge();
//		charge.setDao(dao);
//		try {
//			charge.GenerateCharge();
//		} catch (Exception e) {
//			System.out.println("定时活动:  自动生成下月定额失败！" + e.getMessage());
//		}
		
		System.out.println(">>>> 结束活动:  .............." + StaticTools.getDateTime());
	}
}
